package com.kd.kdaicodemother.ai;

import com.kd.kdaicodemother.utils.SpringContextUtil;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.service.AiServices;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

/**
 * AI代码生成类型路由服务工厂
 *
 * @author yupi
 */
@Slf4j
@Component
public class AiCodeGenTypeRoutingServiceFactory {


    private final ApplicationContext applicationContext;

    public AiCodeGenTypeRoutingServiceFactory(@Lazy ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    /**
     * 每次调用都拿到一个新的 RoutingService（内含新的 prototype ChatModel）
     */
    public AiCodeGenTypeRoutingService createAiCodeGenTypeRoutingService() {
        // 这里 Spring 会返回一个全新的 prototype 实例
        ChatModel chatModel = applicationContext.getBean("routingChatModelPrototype", ChatModel.class);
        return AiServices.builder(AiCodeGenTypeRoutingService.class)
                .chatModel(chatModel)
                .build();
    }

    /**
     * 默认提供一个 Bean
     */
    @Bean
    public AiCodeGenTypeRoutingService aiCodeGenTypeRoutingService() {
        return createAiCodeGenTypeRoutingService();
    }
}

